package com.tianzmp.ttq.admin.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URLDecoder;

/**
 * @author zhumengping
 * @title: ResourceUtils
 * @projectName ttq-admin
 * @date 2023/10/24 10:18
 */
public class ResourceUtils {


    private static ResourceUtils instance;

    private ResourceUtils(){}

    public static ResourceUtils getInstance(){
        if (null == instance){
            instance = new ResourceUtils();
        }
        return instance;
    }




    public String printFileContent(Object obj) throws IOException{
        if (null == obj){
            throw new RuntimeException("params error");
        }
        BufferedReader reader = null;
        if (obj instanceof String){
            reader = new BufferedReader(new FileReader(new File((String)obj)));
        }
        if (obj instanceof InputStream){
            reader = new BufferedReader(new InputStreamReader((InputStream) obj));
        }
        String result = "";
        String line = null;
        while ((line = reader.readLine()) != null){
            result = line;
        }
        reader.close();
        return result;
    }

    public String getResource(String fileName) throws IOException{
        InputStream in = this.getClass().getClassLoader().getResourceAsStream(fileName);
        return printFileContent(in);
    }

    public String getResourcePath(String fileName) throws IOException{
        String path = this.getClass().getClassLoader().getResource(fileName).getPath();
        String decode = URLDecoder.decode(path, "utf-8");
        return decode;
    }

    public static void main(String[] args) throws IOException {
        String resourcePath = ResourceUtils.getInstance().getResourcePath("img");
        System.out.println(resourcePath);
    }
}
